package com.ha8.netease.views.team.home
{
	import com.ha8.netease.App;
	import com.ha8.netease.PrivateNotify;
	import com.ha8.netease.models.WeiboProxy;
	import com.ha8.netease.models.vo.HomeReqVO;
	import com.ha8.netease.models.vo.MicroblogModel;
	import com.ha8.netease.views.utils.DataUtil;
	import com.ha8.netease.views.utils.SortUtil;
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	
	/**
	 * 首页视图层代理
	 * 
	 * @author rogo
	 */	
	public class HomeViewMediator extends Mediator
	{
		public function HomeViewMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'homeViewMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var refreshTimer:Timer;
		private var timelineVO:HomeReqVO;
		private var since_id:String = null;
		private var max_id:String = null;
		private var isloading:Boolean = false;
		
		/**
		 * 视图显示的微博数组
		 */		
		private var displayMicroblogs:ArrayCollection;
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function onRegister():void
		{
			displayMicroblogs = new ArrayCollection();
			displayMicroblogs.sort = SortUtil.getMicroblogSort();
			view.dataGroup.dataProvider = displayMicroblogs;
			
			view.scroller.verticalScrollBar.addEventListener(Event.CHANGE, scroller_changeHandler);
			
			timelineVO = new HomeReqVO();
			requestData();
			
			autoRefresh();
		}
		
		override public function listNotificationInterests():Array
		{
			return [PrivateNotify.GET_HOMETIMELINE_RESULT, 
				PrivateNotify.GET_HOMETIMELINE_ERROR];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			switch (notification.name)
			{
				case PrivateNotify.GET_HOMETIMELINE_RESULT:
					updateData(notification.param as ArrayCollection);
					isloading = false;
					break;
				case PrivateNotify.GET_HOMETIMELINE_ERROR:
					isloading = false;
					break;
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function get view():HomeView
		{
			return viewComponent as HomeView;
		}
		
		public function get proxy():WeiboProxy
		{
			return facade.retrieveProxy(WeiboProxy.NAME) as WeiboProxy;
		}
		
		/**
		 * 更新数据
		 */		
		public function updateData(data:ArrayCollection):void
		{
			if (!data || data.length == 0)
				return;
			
			DataUtil.merge(displayMicroblogs, data);
			displayMicroblogs.refresh();
			
			// 记录游标
			if (displayMicroblogs.length > 0)
			{
				max_id = MicroblogModel(displayMicroblogs[0]).cursor_id;
				since_id = MicroblogModel(displayMicroblogs[displayMicroblogs.length - 1]).cursor_id;
			}
		}
		
		/**
		 * 开始请求数据
		 */		
		private function requestData():void
		{
			if (isloading)
				return;
			
			proxy.getHomeTimeline(timelineVO);
			isloading = true;
		}
		
		/**
		 * 自动更新
		 */		
		public function autoRefresh():void
		{
			if (refreshTimer)
				return;
			
			refreshTimer = new Timer(App.REFRESH_INTERVAL);
			refreshTimer.addEventListener(TimerEvent.TIMER, onTimerHandler);
			refreshTimer.start();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function scroller_changeHandler(e:Event):void
		{
			// 滚到最底部 获取更多数据
			if (view.content.verticalScrollPosition >= 
				view.scroller.verticalScrollBar.maximum)
			{
				// 获取since_id之前发的微博列表
				timelineVO.since_id = since_id;
				timelineVO.max_id = null;
				requestData();
			}
		}
		
		private function onTimerHandler(e:TimerEvent):void
		{
			// 获取max_id之后发的微博列表
			timelineVO.since_id = null;
			timelineVO.max_id = max_id;
			requestData();
		}
		
	}
}